package com.cqs.learning.spring.cloud.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqs.learning.spring.cloud.producer.bean.po.HousePrice;
import com.cqs.learning.spring.cloud.producer.mapper.HousePriceMapper;
import com.cqs.learning.spring.cloud.producer.service.HousePriceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author lixw
 * @Date 6/2/20 8:33 AM
 */
@Slf4j
@Service
@Transactional
public class HousePriceServiceImpl extends ServiceImpl<HousePriceMapper, HousePrice>
        implements HousePriceService {


    /**
     * 翻页查询
     *
     * @param housePrice
     * @return
     */
    public Page<HousePrice> page(HousePrice housePrice) {
        Page<HousePrice> page = new Page<>();
        page.setCurrent(housePrice.getPageNo());
        page.setSize(housePrice.getPageSize());
        QueryWrapper<HousePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(housePrice);
        //模糊查询
        if (StringUtils.isNotBlank(housePrice.getTownOrCity())) {
            queryWrapper.like("town_or_city", housePrice.getTownOrCity());
            housePrice.setTownOrCity(null);
        }
        //区间查询
        if (housePrice.getTransferStart() != null && housePrice.getTransferEnd() != null) {
            queryWrapper.between("transfer_date", housePrice.getTransferStart(), housePrice.getTransferEnd());
            housePrice.setTransferDate(null);
        }
        queryWrapper.orderByDesc("transfer_date");
        return page(page, queryWrapper);
    }
}
